package com.googlecode.gwtbb.client.only.widget.form.entry;

import com.google.gwt.user.client.ui.ComplexPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.googlecode.gwtbb.client.shared.util.ObjectUtils0;
import com.googlecode.gwtbb.client.shared.util.StringUtils;
import com.googlecode.gwtbb.client.shared.validation.Failure;
import com.googlecode.gwtbb.client.shared.validation.FieldId;

/**
 * @author Abraham Grief
 * @version $Id$
 * @since Apr 30, 2008
 */
public abstract class FormEntryImpl implements FormEntry {
	protected FieldId fieldId;
	protected VerticalPanel failurePanel = new VerticalPanel();
	protected String defaultValue;
	protected boolean displayedInitially = true;
	protected int tabIndex = NO_TABS;
	protected String subFormName;

	public String getDefaultValue() {
		return defaultValue;
	}

	public void setDefaultValue(String defaultValue) {
		this.defaultValue = defaultValue;
	}

	public ComplexPanel getFailurePanel() {
		return failurePanel;
	}

	public FieldId getFieldId() {
		return fieldId;
	}

	public void setFieldId(FieldId fieldId) {
		this.fieldId = fieldId;
	}

	public String getSubFormName() {
		return subFormName;
	}

	public void setSubFormName(String subFormName) {
		this.subFormName = subFormName;
	}

	public int getTabIndex() {
		return tabIndex;
	}

	public void setTabIndex(int tabIndex) {
		this.tabIndex = tabIndex;
	}

	public boolean isDisplayedInitially() {
		return displayedInitially;
	}

	public void setDisplayedInitially(boolean displayedInitially) {
		this.displayedInitially = displayedInitially;
	}

	public void addFailure(Failure f) {
		Label failureLabel = new Label(f.getMessage());
		failureLabel.setStylePrimaryName("gwtbb-formError");
		failurePanel.add(failureLabel);
	}

	public void clearFailures() {
		failurePanel.clear();
	}

	public void clearValue() {
		setValue(defaultValue);
	}

	public abstract Widget getEntryWidget();

	public boolean isChanged() {
		return !StringUtils.emptySafeEquals(getValue(), getDefaultValue());
	}
}
